package cn.dglydrpy.study.j2ee.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dglydrpy.study.j2ee.manage.dao.UserDAO;
import cn.dglydrpy.study.j2ee.manage.entity.User;


public class LoginServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name").trim();
		String pwd = request.getParameter("pwd").trim();
		
		UserDAO dao = new UserDAO();
		try{
			User user = dao.findByUsername(name);
			if(user.getUsername()==null){
				request.setAttribute("msg", "用户不存在,请重新登录！");
				request.getRequestDispatcher("login5.jsp").forward(request, response);
			}else if(user.getPassword().equals(pwd)){
				response.sendRedirect("listUser");
			}else{
				request.setAttribute("msg", "密码不正确,请重新登录！");
				request.getRequestDispatcher("login5.jsp").forward(request, response);
			}
			//注意: 转发以后不能再处理网页内容, 包括向response输出信息, 或者再次转发!!
		}catch(Exception e){
			request.setAttribute("msg", "系统繁忙，请稍后重试");
			request.getRequestDispatcher("login5.jsp").forward(request, response);
		}	
	}
}
